/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.geronimo.st.v30.core.commands;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.enterprise.deploy.shared.CommandType;
import javax.enterprise.deploy.spi.status.DeploymentStatus;
import javax.enterprise.deploy.spi.status.ProgressEvent;
import javax.enterprise.deploy.spi.status.ProgressListener;
import javax.enterprise.deploy.spi.status.ProgressObject;

import org.apache.geronimo.st.v30.core.Activator;
import org.apache.geronimo.st.v30.core.DeploymentStatusMessage;
import org.apache.geronimo.st.v30.core.internal.Trace;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.server.core.IModule;

/**
 * This class is a wrapper IDeploymentCommand that when executed blocks the
 * callee's thread until completed, either when the waiting thread times out, or
 * a failed or completed notification is recieved from the DeploymentManager.
 *
 * @version $Rev$ $Date$
 */
public class SynchronizedDeploymentOp implements ProgressListener,
        IDeploymentCommand {

    private final IDeploymentCommand command;
    
    private MultiStatus status = null;

    private IProgressMonitor _monitor = null;
    
    private CountDownLatch latch = null;

    public SynchronizedDeploymentOp(IDeploymentCommand command) {
        this.command = command;      
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.geronimo.core.commands.IDeploymentCommand#execute(org.eclipse.core.runtime.IProgressMonitor)
     */
    public IStatus execute(IProgressMonitor monitor) throws Exception {

        _monitor = monitor;

        if (_monitor == null) {
            _monitor = new NullProgressMonitor();
        }

        latch = new CountDownLatch(1);
        
        ProgressObject po = run();

        return new DeploymentCmdStatus(status, po);
    }

    private ProgressObject run() throws Exception {
        Trace.trace(Trace.INFO, "--> run()", Activator.traceCommands);
        
        IStatus ds = command.execute(_monitor);

        ProgressObject po = null;

        if (ds instanceof DeploymentCmdStatus) {
            po = ((DeploymentCmdStatus) ds).getProgressObject();
            po.addProgressListener(this);

            boolean timedOut = false;
            try {
                timedOut = !latch.await(getTimeout(), TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
            }

            po.removeProgressListener(this);
            if (timedOut) {
                Trace.trace(Trace.ERROR, "Command Timed Out!", Activator.logCommands);
                status = new MultiStatus(Activator.PLUGIN_ID, 0, "", null);
                status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, command.getCommandType() + " timed out.", null));
            }
        }

        Trace.trace(Trace.INFO, "<-- run()", Activator.traceCommands);
        return po;
    }

    private void sendNotification() {
        Trace.trace(Trace.INFO, "sendNotification()", Activator.traceCommands);
        latch.countDown();
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.enterprise.deploy.spi.status.ProgressListener#handleProgressEvent(javax.enterprise.deploy.spi.status.ProgressEvent)
     */
    public void handleProgressEvent(ProgressEvent event) {
        DeploymentStatus deploymentStatus = event.getDeploymentStatus();
        if (deploymentStatus != null) {
            DeploymentStatusMessage dsm = new DeploymentStatusMessage(deploymentStatus);
            Trace.trace(Trace.INFO, dsm.toString(), Activator.traceCommands);
            _monitor.subTask(dsm.toString());
            if (command.getCommandType() == deploymentStatus.getCommand()) {
                if (deploymentStatus.isCompleted()) {
                    messageToStatus(IStatus.OK, dsm.getMessage(), false);
                    sendNotification();
                } else if (deploymentStatus.isFailed()) {
                    messageToStatus(IStatus.ERROR, dsm.getMessage(), true);
                    sendNotification();
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.geronimo.core.commands.IDeploymentCommand#getCommandType()
     */
    public CommandType getCommandType() {
        return command.getCommandType();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.geronimo.core.commands.IDeploymentCommand#getModule()
     */
    public IModule getModule() {
        return command.getModule();
    }
    
    public long getTimeout() {
        return command.getTimeout();
    }
    
    private void messageToStatus(int severity, String source, boolean error) {
        status = new MultiStatus(Activator.PLUGIN_ID, 0, "", null);
        try {
            BufferedReader in = new BufferedReader(new StringReader(source));
            String line;
            while ((line = in.readLine()) != null) {
                status.add(new Status(severity, Activator.PLUGIN_ID, 0,line, null));
            }
        } catch (IOException e) {

        }
    }
}
